package cn.bvin.lib.app;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import cn.bvin.lib.debug.SimpleLogger;
import cn.bvin.lib.module.net.RequestListener;
import cn.bvin.lib.module.net.RequestOperate;
import cn.bvin.lib.module.net.volley.BaseRequest;
import cn.bvin.lib.module.net.volley.RequestManager;

import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;


public class RequestActivity<T> extends NetActivity implements RequestOperate<T>,RequestListener<T>,Response.ErrorListener, Listener<T>{

	private Request<T> request;// 当前请求，保持唯一
	//protected PlaceViewHolder mPlaceViewHolder;// 请求占位视图
	
	protected AbsRequestHolderFragment mRequestHolderFragment;
	
	@Override
	public void addRequest(Request<T> request) {
		if (request==null) {
  	        SimpleLogger.log_w("addRequest", "resquest为Null");
  	        return;
        }
		if (TextUtils.isEmpty(request.getUrl())) {
			SimpleLogger.log_w("addRequest", "不存在URL的请求");
		} else {
			this.request = request;
			onRequestStart(request);
			RequestManager.addRequest(request);
		}
	}

	@Override
	public void cancelRequest() {
		if (request != null)
			request.cancel();
	}

	@Override
	public void reloadRequest() {
		addRequest(request);
	}

	
    @SuppressWarnings("unchecked")
    @Override
    public void onRequestStart(Request<T> resquest) {
        if (resquest instanceof BaseRequest)
        {
            SimpleLogger.log_i("onRequestStart", ((BaseRequest<T>) resquest).getUrlWithParams());
        } else
        {
            SimpleLogger.log_i("onRequestStart", resquest.getUrl());
        }
        // 先让请求占位重置视图
        if (mRequestHolderFragment != null){
            mRequestHolderFragment.resetHoldView();
            //mRequestHolderFragment.showLoadingProgress();
        }

        // 再找出并通知包含的RequestFragment回调onRequestStart
        if (getSupportFragmentManager().getFragments()!=null) {
            for (Fragment fragment : getSupportFragmentManager().getFragments()) {
                if (fragment instanceof RequestFragment) {
                    if (((RequestFragment<T>) fragment).isCurrentRequest()) {
                        ((RequestFragment<T>) fragment).onRequestStart(request);
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onRequestSuccess(T result) {
        // 返回成功不一定就真的成功，这里移交到具体的类去处理
        if (mRequestHolderFragment != null)// 请求成功把LoadingView设置为GONE
            mRequestHolderFragment.hideLoadingProgress();
        // 再找出并通知包含的RequestFragment回调onRequestStart
        if (getSupportFragmentManager().getFragments()!=null) {
            for (Fragment fragment : getSupportFragmentManager().getFragments()) {
                if (fragment instanceof RequestFragment) {
                    if (((RequestFragment<T>) fragment).isCurrentRequest()) {
                        ((RequestFragment<T>) fragment).onRequestSuccess(result);;
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onRequestFailure(VolleyError error) {
        if (mRequestHolderFragment != null) {
            mRequestHolderFragment.hideLoadingProgress();
            if (error instanceof NoConnectionError) {// 错误界面
                mRequestHolderFragment.setErrorTips("服务器连接失败");
            } else if (error instanceof TimeoutError) {
                mRequestHolderFragment.setErrorTips("网络超时");
            } else if (error instanceof ParseError) {
                mRequestHolderFragment.setErrorTips("数据解析异常");
            } else if (error instanceof ServerError) {
                mRequestHolderFragment.setErrorTips("服务器异常");
            } else {
                mRequestHolderFragment.setErrorTips("未知错误");
            }
        }
        // 再找出并通知包含的RequestFragment回调onRequestFailure
        if (getSupportFragmentManager().getFragments()!=null) {
            for (Fragment fragment : getSupportFragmentManager().getFragments()) {
                if (fragment instanceof RequestFragment) {
                    if (((RequestFragment<T>) fragment).isCurrentRequest()) {
                        ((RequestFragment<T>) fragment).onRequestFailure(error);;
                    }
                }
            }
        }
    }

    @Override
    public void onResponse(T arg0) {
        onRequestSuccess(arg0);
    }

    @Override
    public void onErrorResponse(VolleyError arg0) {
        onRequestFailure(arg0);
    }

	
}
